<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;

class AjaxController extends SecBaseController
{
    public function actionCall(){
        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $thisPath = $request->getPathInfo();
        $path_array = explode("/", $thisPath);//0:ajax, 1:call, 2,3:后端服务url 
        $thisRequestParams = $request->getQueryParams();
        $thisBodyParams = $request->getBodyParams();

        $req_params =  array_merge($this->remote_params, $thisRequestParams, $thisBodyParams);
        $new_params =  array();
        foreach ($req_params as $key => $value) {
            if(is_array($value)){
                foreach ($value as $k => $v) {
                    if(isset($new_params[$key])){
                        $new_params[$key] = $new_params[$key].','.$v;
                    }else{
                        $new_params[$key] = $v;
                    }
                    
                }
            }else{
                $new_params[$key] = $value;
            }
        }
       // var_dump($new_params) ;exit ; 
        try{ 
            $ret = $this->proxy->execute($path_array[2], $path_array[3],$new_params );
        }catch(\Exception $e){
            // $this->return_page_values['errno'] = $e->getCode();
            // $this->return_page_values['errmsg'] = $e->getMessage();
            // return $this->myRenderFile('@tpl/page/error.tpl', $this->return_page_values);
             return ['errno' => $e->getCode(), 'errmsg' => $e->getMessage()];
        }

        return $ret;

    }


    
}
